class Solution {
public:
    int minOperations(vector<int>& nums) {
        int n=nums.size();
        sort(nums.begin(),nums.end());
        nums.erase(unique(nums.begin(),nums.end()),nums.end());
        int sz=nums.size();
        int ans=n;
        for(int i=0,j=0;i<sz;i++)
        {
            while(j+1<sz&&nums[j+1]<=nums[i]+n-1) j++;
            ans=min(ans,n-(j-i+1));
        }
        return ans;
    }
};